﻿using System;
using System.Collections.Generic;
using System.Linq;
using ComponentFactory.Krypton.Toolkit;
using System.Data;
using System.Windows.Forms;
using System.Drawing;

namespace OAuthClient
{
    /// <summary>
    /// 
    /// </summary>
    public partial class TextForm : KryptonForm
    {
        /// <summary>
        /// 
        /// </summary>
        public TextForm()
        {
            InitializeComponent();
            _paramsGridView.CellFormatting += OnParamsGridViewCellFormatting;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void OnParamsGridViewCellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            // compare the column to the column you want to format
            if (_paramsGridView.Columns[e.ColumnIndex] == _keyDgvColumn)
            {
                var dr = (_paramsGridView.Rows[e.RowIndex].DataBoundItem as DataRowView).Row;
                e.CellStyle.BackColor =
                    string.Compare(dr["header"].ToString(), "1", false) == 0 ?
                        Color.LemonChiffon : SystemColors.Window;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="content"></param>
        public TextForm(string content)
            : this()
        {
            _contentTextBox.Text = content;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="content"></param>
        public TextForm(LogViewItem item)
            : this()
        {
            var dt = new DataTable();
            dt.Columns.Add(new DataColumn("order"));
            dt.Columns.Add(new DataColumn("key"));
            dt.Columns.Add(new DataColumn("content"));
            dt.Columns.Add(new DataColumn("header"));

            _paramsGridView.AutoGenerateColumns = false;
            _paramsGridView.DataSource = dt;

            _contentTextBox.Text = item.Message;
            Array.ForEach(item.Headers.AllKeys, key =>
            {
                var dr = dt.NewRow();
                dr["order"] = (dt.Rows.Count + 1).ToString();
                dr["key"] = key;
                dr["content"] = item.Headers[key];
                dr["header"] = 1;
                dt.Rows.Add(dr);
            });

            var nameValuePairs = item.Extra != null ? item.Extra.Split(new char[] { '&' },
                StringSplitOptions.RemoveEmptyEntries) : new string[] { };
            Array.ForEach(nameValuePairs, nameValuePair =>
            {
                var nameValuePairParts = nameValuePair.Split(new char[] { '=' },
                    StringSplitOptions.RemoveEmptyEntries);
                if (nameValuePairParts.Length == 2)
                {
                    var dr = dt.NewRow();
                    dr["order"] = (dt.Rows.Count + 1).ToString();
                    dr["key"] = nameValuePairParts[0];
                    dr["content"] = nameValuePairParts[1];
                    dr["header"] = 0;
                    dt.Rows.Add(dr);
                }
                else if (nameValuePairParts.Length == 1)
                {
                    var dr = dt.NewRow();
                    dr["order"] = (dt.Rows.Count + 1).ToString();
                    dr["key"] = nameValuePairParts[0];
                    dr["header"] = 0;
                    dt.Rows.Add(dr);
                }
            });
        }
    }
}
